package com.audaque.springboot.foshanupload.core.model.qo.page;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

@Schema(description = "分页响应")
@Data

@NoArgsConstructor
@AllArgsConstructor

public class Page<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    //前端
    private Integer page;// 页码，从1开始
    private Integer rows;// 页大小

    //数据库
    private Integer offset;// 下标，偏移量，从0开始
    private Integer limit;//多少行


    private boolean calculateCount = true;// 是否需要统计总行数

    private Integer total;// 总行数，当不计算总行数时，-1表示还有更多数据，正数表示没有了
    private List<T> records;

    public void setPage(Integer page) {
        this.page = page;
        this.initPage();
    }

    public void setRows(Integer rows) {
        this.rows = rows;
        this.initPage();
    }


    public static <T> Page<T> maxPage() {
        Page<T> p = new Page<T>();
        p.setOffset(0);
        p.setLimit(Integer.MAX_VALUE);
        p.setPage(1);
        p.setRows(Integer.MAX_VALUE);
        return p;
    }


    public void initPage()  {
        if (this.page != null && this.rows != null && this.page > 0 && this.rows > 0) {
            this.setOffset((this.page - 1) * this.rows);
            this.setLimit(this.rows);
        }
    }


}
